/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.workbench.tests.contexts;

import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.tests.harness.util.UITestCase;

import java.util.Collection;

/**
 *
 * PartContextTest.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class PartContextTest extends UITestCase {
    public static final String PERSPECTIVE_ID = "com.cisco.nm.cmp.client.ui.test.ContextTestPerspective";
    public static final String WINDOW_CONTEXT_ID = "com.cisco.nm.cmp.client.ui.workbench.window";
    public static final String WB_CONTEXT_ID = "com.cisco.nm.cmp.client.ui.workbench";

    public PartContextTest(String name) {
        super(name);
    }

    //	public void testBasicContextActivation() throws Exception {
    //
    //		IContextService globalService = (IContextService) getWorkbench()
    //				.getService(IContextService.class);
    //
    //		checkActiveContext(globalService, WB_CONTEXT_ID, true);
    //
    //		boolean windowOpen = false;
    //		if (getWorkbench().getWorkbenchWindowCount() > 0)
    //			windowOpen = true;
    //		// here's window context has only been set.
    //		checkActiveContext(globalService, WINDOW_CONTEXT_ID, windowOpen);
    //
    //		// ensure that part context is not active.
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, false);
    //
    //		IWorkbenchWindow window = openTestWindow(PERSPECTIVE_ID);
    //		IWorkbenchPage page = window.getActivePage();
    //
    //		IViewPart view = page.showView(ASVContextPart.ID);
    //
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, true);
    //
    //		page.hideView(view);
    //
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, false);
    //
    //		closeAllPages(window);
    //		closeAllTestWindows();
    //
    //	}
    //
    //	public void testContextActivation() throws Exception {
    //
    //		IContextService globalService = (IContextService) getWorkbench()
    //				.getService(IContextService.class);
    //
    //		checkActiveContext(globalService, WB_CONTEXT_ID, true);
    //
    //		boolean windowOpen = false;
    //		if (getWorkbench().getWorkbenchWindowCount() > 0)
    //			windowOpen = true;
    //		// here's window context has only been set.
    //		checkActiveContext(globalService, WINDOW_CONTEXT_ID, windowOpen);
    //
    //		// ensure that part context is not active.
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, false);
    //		checkActiveContext(globalService, CSVContextPart.CONTEXT_ID, false);
    //
    //		IWorkbenchWindow window = openTestWindow(PERSPECTIVE_ID);
    //		IWorkbenchPage page = window.getActivePage();
    //
    //		IViewPart asvView = page.showView(ASVContextPart.ID);
    //		IContextService localService = (IContextService) asvView.getSite()
    //				.getService(IContextService.class);
    //
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, true);
    //		checkActiveContext(localService, ASVContextPart.CONTEXT_ID, true);
    //
    //		IViewPart csvView = page.showView(CSVContextPart.ID);
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, false);
    //		checkActiveContext(localService, ASVContextPart.CONTEXT_ID, false);
    //
    //		page.activate(asvView);
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, true);
    //
    //		page.activate(csvView);
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, false);
    //
    //		page.hideView(csvView);
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, true);
    //
    //		page.hideView(asvView);
    //		checkActiveContext(globalService, ASVContextPart.CONTEXT_ID, false);
    //
    //		closeAllPages(window);
    //		closeAllTestWindows();
    //
    //	}
    public void testWindowContextActivation() throws Exception {
        IContextService globalService = (IContextService) getWorkbench()
                                                              .getService(IContextService.class);

        checkActiveContext(globalService, WB_CONTEXT_ID, true);

        boolean windowOpen = false;

        if (getWorkbench().getWorkbenchWindowCount() > 0)
            windowOpen = true;

        // here's window context has only been set.
        checkActiveContext(globalService, WINDOW_CONTEXT_ID, windowOpen);

        IWorkbenchWindow window = openTestWindow();
        IContextService localService = (IContextService) window.getService(IContextService.class);
        localService.activateContext(WINDOW_CONTEXT_ID);
        checkActiveContext(globalService, WINDOW_CONTEXT_ID, true);

        window.close();
    }

    // /**
    // * Test context activation while switching through the pages of a
    // pagebook.
    // * Exercises the NestableContextService.
    // *
    // * @throws Exception
    // * on error
    // */
    // public void testPageBookPageContextActivation() throws Exception {
    // IContextService globalService = (IContextService) getWorkbench()
    // .getService(IContextService.class);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, false);
    //
    // IWorkbenchWindow window = openTestWindow();
    // IWorkbenchPage page = window.getActivePage();
    //
    // IViewPart pageView = page.showView(PAGE_VIEW_ID);
    // assertNotNull(pageView);
    //
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, false);
    //
    // IProject proj = FileUtil.createProject("ContextTest");
    // IFile test01 = FileUtil.createFile("test01.txt", proj);
    // IEditorPart editor01 = page.openEditor(new FileEditorInput(test01),
    // TEXT_EDITOR_ID);
    // assertNotNull(editor01);
    // page.activate(pageView);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, false);
    //
    // IFile test02 = FileUtil.createFile("test02.xml", proj);
    // IEditorPart editor02 = page.openEditor(new FileEditorInput(test02),
    // TEXT_EDITOR_ID);
    // assertNotNull(editor02);
    // page.activate(pageView);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, true);
    //
    // page.activate(editor01);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, false);
    // page.activate(pageView);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, false);
    //
    // page.activate(editor02);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, false);
    // page.activate(pageView);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, true);
    // page.activate(editor02);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, false);
    // page.activate(pageView);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, true);
    //
    // page.activate(editor01);
    // page.activate(pageView);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, false);
    //
    // page.activate(editor01);
    // page.closeEditor(editor01, false);
    // page.activate(pageView);
    // checkActiveContext(globalService, ContextPage.TEST_CONTEXT_ID, true);
    // }

    /**
     * Assert if the contextId is active in the contextService.
     *
     * @param contextService
     * @param contextId
     * @param isActive
     */
    private void checkActiveContext(IContextService contextService,
        String contextId, boolean isActive) {
        Collection activeContexts = contextService.getActiveContextIds();
        assertEquals(contextId, isActive, activeContexts.contains(contextId));
    }
}
